package site.lyweb.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import site.lyweb.domain.dto.PageDTO;
import site.lyweb.domain.dto.StuCourseRelation;
import site.lyweb.domain.dto.TeaCourseRelation;
import site.lyweb.domain.dto.StudentCourseDTO;
import site.lyweb.domain.query.StudentCourseQuery;
import site.lyweb.result.RestResult;
import site.lyweb.service.StudentCourseService;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Author 林瑶
 * @Description TODO
 * @Date 2024/6/20 2:21
 */
@CrossOrigin(origins = "*")
@RestController
@Tag(name="学生选课管理模块")
@Slf4j
@RequestMapping("/student_course")
public class StudentCourseController {
    @Resource
    private StudentCourseService studentCourseService;
    /**
     * 分页查询学生选课列表数据
     *
     * @return
     */

    @Operation(summary = "分页查询学生选课列表数据")
    @GetMapping(value = "/student_course_info/getStudentCourseByPage")
    public RestResult getStudentCourseByPage( StudentCourseQuery studentCourseQuery) {


        //分页查询的结果用PageDTO进行封装
        PageDTO<StudentCourseDTO> pageDTO = studentCourseService.getStudentCourseByPage(0, studentCourseQuery);

        //有的时候，查询可以不判断成功与失败，不管查没查到数据，都返回200，页面上去控制显示
        return RestResult.SUCCESS(pageDTO);
    }
    /**
     * 根据id批量学生选课信息表中信息
     */
    @Operation(summary = "根据ids删除学生选课信息表中信息")
    @Parameters({
            @Parameter(name = "ids",description = "id(主键)",in = ParameterIn.PATH)
    })
    @CrossOrigin
    @PostMapping("/student_course_info/batchDel/{ids}")
    public RestResult batchDelStudentCourseById(@PathVariable("ids") String ids){
        //ids = "1,2,5,7,10,12" , ids.split(",")是把ids转成数组, Arrays.asList()是把数组转成List
        List<String> idList = Arrays.asList(ids.split(","));
        return  studentCourseService.removeBatchByIds(idList) ? RestResult.SUCCESS() : RestResult.FAIL();
    }
    /**
     * 根据id学生选课信息表中信息
     */
    @Operation(summary = "根据id删除学生选课信息表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @CrossOrigin
    @PostMapping("/student_course_info/del/{id}")
    public RestResult delStudentCourseById(@PathVariable("id") String id){
        return  studentCourseService.removeById(id) ? RestResult.SUCCESS() : RestResult.FAIL();
    }
    /**
     * 保存学生选课信息表中信息
     */
    @Operation(summary = "保存学生选课信息表中信息")
    @PostMapping("/student_course_info/save")
    @Parameters({
            @Parameter(name = "id",description = "id（主键）",in = ParameterIn.QUERY),
            @Parameter(name = "stuId",description = "学生id",in = ParameterIn.QUERY),
            @Parameter(name = "courseId",description = "课程id",in = ParameterIn.QUERY),
            @Parameter(name = "teacherId",description = "老师id",in = ParameterIn.QUERY),
    })
    public RestResult saveStudentCourse(StuCourseRelation stuCourseRelation){
        String id = stuCourseRelation.getId();
        String studentId = stuCourseRelation.getStuId();
        String courseId = stuCourseRelation.getCourseId();
        String teacherId = stuCourseRelation.getTeacherId();
        BigDecimal courseScore = stuCourseRelation.getCourseScore();
        System.out.println("看看正确嘛："+stuCourseRelation);
        return studentCourseService.save(id,studentId,courseId,teacherId,courseScore)?RestResult.SUCCESS("保存学生选课信息表中信息成功"):RestResult.FAIL("保存学生选课信息表中信息失败");
    }
    /**
     * 根据id查询选课信息表中信息
     */
    @Operation(summary = "根据id查询学生选课信息表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @CrossOrigin
    @GetMapping("/student_course_info/get/{id}")
    public RestResult getStudentCourseById(@PathVariable("id") String id){
        try{
            return RestResult.SUCCESS(studentCourseService.getById(id)) ;
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.FAIL("查询失败");
        }

    }
}
